# 爬取分类页

# ret=[]; $('.typecont a').each(function(){ ret.push({ title: $(this).parent().parent().find('strong').text(), url: this.href}) }); JSON.stringify(ret)
# {'title': '先秦', 'url': 'https://so.gushiwen.cn/gushi/xianqin.aspx'}

import json

from database import Database
from entities import *
from poem import get_poem

json_text = """
[{"title":"","url":"https://so.gushiwen.cn/shiwenv_58313be2d918.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_8f1be8b774c2.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d61bf6c27218.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_9a9463173105.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d89eb1125b6d.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_a24c4f626d63.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f4f3de69130c.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fc0f5406ef57.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_257be0e7811c.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_4bb194abd528.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_038457ce8c4e.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f36d338e1fc5.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_7efd4d57fa64.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_ecc9acd2b27a.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_6cbb08b97493.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_562777d53e15.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_b629f4ffa566.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_62fcb0fd4611.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fdc3a79d25a8.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_22861b058f71.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d94cffe12180.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f22961a480dc.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_65b0be6fd721.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_0e1671eb861a.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_7aed86da657c.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_1ba8e6633557.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_42ac91f4cb4e.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_dd9d562de905.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_25d7014db155.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fdae6ac54db6.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d3ba5a3a4d29.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fc0c91c784cb.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f2c4a1d51fea.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_1d3c1894b604.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_da72f5c29195.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_282c957790b1.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_81bbfea79f42.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fd6555f7e9fa.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_754a21b5caaf.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_1498456b8c42.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_e3d42bc56fff.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_982f5a676262.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_bca77bd4b20d.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_e9da3f3c53f6.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f62cf2ab2652.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d1e82216bd30.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_c79b5777d681.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_28875e696250.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_0d24da014fe2.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_0eab34c483bb.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_5a65da8c1ae2.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_3baff3af415c.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_b39f6213d3bb.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_e396ed5b2ce3.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_381e229ed864.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_e9167fd09d39.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_c108fef1f57d.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_08f2b0c327c6.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_4b1f53683894.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_62436c033536.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_b19622516dd0.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_697f875a4401.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f3580d2413ce.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_f8beeba28d5f.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_6c5158679e5e.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_8e1f3863e32d.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_b4780acded14.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_fc520d5da81c.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_201bbc14d199.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_c2aaf72284e8.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_62789584d6fb.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_337ebe71980d.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_5bcb77d51306.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_45e2c21bcee2.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_87ed22765e07.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_47eef4bcad87.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_b6207833b260.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_24416dff3820.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_d9eb755a9802.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_572704722a5a.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_da110a64cc5e.aspx"},{"title":"","url":"https://so.gushiwen.cn/shiwenv_453266e63bf1.aspx"}]


"""

db = Database()

array = json.loads(json_text)

for item in array:
    item['title'] = '冬天' + item['title']
    if db.find_tag_by_name(item['title']) is None:
        tag = Tag(name=item['title'])
        db.create_tag(tag)
    else:
        tag = db.find_tag_by_name(item['title'])

    url = item['url']
    while True:
        try:
            poem = get_poem(url)
            break
        except:
            import traceback
            traceback.print_exc()
    poem_tag = PoemTag(poem_id=poem.poem_id, tag_id=tag.tag_id)
    db.create_poem_tag(poem_tag)
    print(poem)
